// ==UserScript==
// @name         51CTO复制
// @version      0.0.2
// @description  51CTO复制,就要复制(凭实力开挂，凭什么不让),强力一键复制、右键复制、Ctrl+C复制，去除小尾巴
// @author       cy2
// @match        *://blog.51cto.com/*
// @homepage     https://gitee.com/caiyaoyuan/script-sets/tree/master
// @namespace    https://gitee.com/caiyaoyuan/script-sets/blob/master/JavaScript/Copy51CTO.js
// @updateURL    https://gitee.com/caiyaoyuan/script-sets/raw/master/JavaScript/Copy51CTO.js
// @downloadURL  https://gitee.com/caiyaoyuan/script-sets/raw/master/JavaScript/Copy51CTO.js
// @icon         https://blog.51cto.com/favicon.ico
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 留着链接，可能什么时候就用到了
    // @require      https://code.jquery.com/jquery-2.1.4.min.js
    // @require      https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js

    function waitObject(getCall, initCall, name){
        let count = 0, time1 = new Date().getTime();
        let pid = setInterval(() => {
            count++;
            let obj = getCall();
            if(obj) {
                clearInterval(pid);
                let time2 = new Date().getTime();
                console.info("等待", name, "对象加载耗时：", (time2 - time1) / 1000, "秒");
                if(typeof(obj) === "function"){
                    initCall();
                }
            }
        }, 500);
    }

    function writeClipboardText(text){
        // console.info(text);
        navigator.clipboard.writeText(text)
            .then(() => {
            console.log("复制成功");
        }).catch((error) => {
            console.log("复制失败", error);
        });
    }

    function init(){
        let $ = window.jQuery || window.$;
        let $body = $("body");
        let $copy_btns = $(".copy_btn").unbind("click").click(function(e) {
            e.preventDefault();
            e.stopPropagation();
            let $target = $(e.target);
            let $ele = $target.parent().parent(".hljs-cto").find("pre").find("code");
            if($ele.length == 0){
                $ele = $target.parent().parent(".hljs-cto").find("pre").find(".language-");
            }
            if($ele.length == 0){
                $ele = $target.parent().parent(".hljs-cto").find("pre");
            }
            let text = $ele[0].innerText;
            writeClipboardText(text);
            $target.html("已复制");
            setTimeout(function() {
                $target.html("复制")
            }, 2e3);
            return false;
        }).each((i, v) => {
            //console.info(i, v.innerHTML);
            v.innerHTML = "复制";
        });
        //console.info($copy_btns)
        $(".article-content-wrap").unbind("keydown")
        $(".article-content-wrap").unbind("copy")
        $("#markdownContent").unbind("copy").on("copy", function(t) {
            writeClipboardText(window.getSelection()?.toString());
        });
    }

    function main(){
        waitObject(() => {
            return window.jQuery;
        }, () => {
            waitObject(() => {
                return window.ClipboardJS;
            }, init, "ClipboardJS");
        }, "jQuery");
    }

    main();
})();